﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;

namespace MipsPipelineSimulator
{
    class IFStage
    {
        //能否运行
        static public int enableRun = 0;

        static private ManualResetEvent manuaEevent = new ManualResetEvent(false);

        static private Thread ifThread = null;

        static public Point[] position = new Point[25];

        static public int codeCurrentAddress = 0;

        static private Code code;

        static private MainForm mainForm = null;

        static private void ThreadFun()
        {
            code = RunTimeCode.GetCode(codeCurrentAddress);

            switch(code.codeType)
            {
                case CodeType.Over:
                    {
                        enableRun--;
                        IDStage.Wait();
                        break;
                    }
                case CodeType.Add:
                    {
                        //做事



                        IDStage.Wait();
                        IDStage.enableRun++;

                        break;
                    }
            }
            
            //结束
            manuaEevent.Set();
        }

        static public void Start(MainForm param)
        {
            mainForm = param;
            //开始标志
            manuaEevent = new ManualResetEvent(false);
            ifThread = new Thread(new ThreadStart(ThreadFun));
            ifThread.Start();
        }

        static public void Wait()
        {
            manuaEevent.WaitOne();
        }

        static public void Close()
        {
            if (ifThread != null && ifThread.IsAlive)
            {
                ifThread.Abort();
            }
        }
    }
}
